<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="编码" prop="code">
      <el-input v-model="dataForm.code" placeholder="编码"></el-input>
    </el-form-item>
    <el-form-item label="创建人" prop="author">
      <el-input v-model="dataForm.author" placeholder="创建人"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新人" prop="modifier">
      <el-input v-model="dataForm.modifier" placeholder="更新人"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="modifyTime">
      <el-input v-model="dataForm.modifyTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="状态,1正常，0无效，-1删除" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态,1正常，0无效，-1删除"></el-input>
    </el-form-item>
    <el-form-item label="日志记录" prop="log">
      <el-input v-model="dataForm.log" placeholder="日志记录"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          author: '',
          createTime: '',
          modifier: '',
          modifyTime: '',
          status: '',
          log: ''
        },
        dataRule: {
          code: [
            { required: true, message: '编码不能为空', trigger: 'blur' }
          ],
          author: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifier: [
            { required: true, message: '更新人不能为空', trigger: 'blur' }
          ],
          modifyTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态,1正常，0无效，-1删除不能为空', trigger: 'blur' }
          ],
          log: [
            { required: true, message: '日志记录不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ledger/log/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.log.code
                this.dataForm.author = data.log.author
                this.dataForm.createTime = data.log.createTime
                this.dataForm.modifier = data.log.modifier
                this.dataForm.modifyTime = data.log.modifyTime
                this.dataForm.status = data.log.status
                this.dataForm.log = data.log.log
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ledger/log/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'author': this.dataForm.author,
                'createTime': this.dataForm.createTime,
                'modifier': this.dataForm.modifier,
                'modifyTime': this.dataForm.modifyTime,
                'status': this.dataForm.status,
                'log': this.dataForm.log
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
